commonlibsse_ng\re\t/
TESGlobal.rs

1//! # TESGlobal
2//!
3//! This module defines the `TESGlobal` struct, which inherits from `TESForm` and represents
4//! global variables in Skyrim's engine. It includes a virtual table for C++ compatibility
5//! and maintains the original memory layout.
6
7use core::ffi::{CStr, c_float};
8
9use crate::re::BSString::{BSString, BSStringError};
10use crate::re::FormTypes::FormType;
11use crate::re::TESForm::{TESForm, TESFormVtbl};
12use crate::re::offsets_rtti::RTTI_TESGlobal;
13use crate::re::offsets_vtable::VTABLE_TESGlobal;
14use crate::rel::id::VariantID;
15
16/// Represents a global variable in Skyrim.
17#[repr(C)]
18#[derive(Debug)]
19pub struct TESGlobal {
20    /// Base class `TESForm`.
21    pub __base: TESForm,
22
23    /// The form editor ID.
24    /// - Offset: `0x20`
25    pub form_editor_id: BSString,
26
27    /// The type of the global (float, long, short).
28    /// - Offset: `0x30`
29    pub type_: Type_CEnum,
30
31    /// Padding for alignment.
32    /// - Offset: `0x31`
33    pub pad31: u8,
34
35    /// Additional padding.
36    /// - Offset: `0x32`
37    pub pad32: u16,
38
39    /// The value of the global variable.
40    /// - Offset: `0x34`
41    pub value: c_float,
42}
43
44const _: () = {
45    assert!(core::mem::offset_of!(TESGlobal, __base) == 0x0);
46    assert!(core::mem::offset_of!(TESGlobal, form_editor_id) == 0x20);
47    assert!(core::mem::offset_of!(TESGlobal, type_) == 0x30);
48    assert!(core::mem::offset_of!(TESGlobal, pad31) == 0x31);
49    assert!(core::mem::offset_of!(TESGlobal, pad32) == 0x32);
50    assert!(core::mem::offset_of!(TESGlobal, value) == 0x34);
51    assert!(core::mem::size_of::<TESGlobal>() == 0x38);
52};
53
54/// The global variable type.
55#[commonlibsse_ng_derive_internal::ffi_enum]
56#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
57#[repr(u8)]
58pub enum Type {
59    /// Stored type is [`f32`].
60    Float = b'f',
61    /// Stored type is [`i32`].
62    Long = b'l',
63    /// Stored type is [`i16`].
64    Short = b's',
65}
66
67impl TESGlobal {
68    /// Address & Offset of the runtime type information (RTTI) identifier.
69    pub const RTTI: VariantID = RTTI_TESGlobal;
70
71    /// Address & Offset of the virtual function table.
72    pub const VTABLE: [VariantID; 1] = VTABLE_TESGlobal;
73
74    pub const FORM_TYPE: FormType = FormType::Global;
75
76    /// Gets the form editor ID as a `&str`.
77    #[inline]
78    pub const fn get_form_editor_id(&self) -> &CStr {
79        self.form_editor_id.as_c_str()
80    }
81
82    /// Sets the form editor ID.
83    ///
84    /// # Errors
85    /// - If the string is too long to fit in a `u16`, or if allocation fails.
86    /// - If allocations fail.
87    #[inline]
88    pub fn set_form_editor_id(&mut self, id: &CStr) -> Result<(), BSStringError> {
89        self.form_editor_id = BSString::from_c_str(id)?;
90        Ok(())
91    }
92}
93
94/// The virtual function table for `TESGlobal`.
95#[repr(C)]
96pub struct TESGlobalVtbl {
97    __base: TESFormVtbl,
98}